package test3.GUI;

import javax.swing.text.*;
import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * User: Fili
 * Date: Sep 1, 2005
 * Time: 9:18:56 AM
 * To change this template use File | Settings | File Templates.
 */
public class UnderlineHighlighter extends DefaultHighlighter
{

	// Shared painter used for default highlighting
	protected static final Highlighter.HighlightPainter sharedPainter = new UnderlineHighlightPainter(null, 0);

	// Painter used for this highlighter
	protected Highlighter.HighlightPainter painter;

	public UnderlineHighlighter(Color c, int width)
	{
		painter = (c == null ? sharedPainter : new UnderlineHighlightPainter(c, width));
	}

	// Convenience method to add a highlight with
	// the default painter.
	public Object addHighlight(int p0, int p1) throws BadLocationException
	{
		return addHighlight(p0, p1, painter);
	}

	public void setDrawsLayeredHighlights(boolean newValue)
	{
		// Illegal if false - we only support layered highlights
		if (!newValue)
		{
			throw new IllegalArgumentException(
					"UnderlineHighlighter only draws layered highlights");
		}
		super.setDrawsLayeredHighlights(true);
	}

	// Painter for underlined highlights
	public static class UnderlineHighlightPainter extends
			LayeredHighlighter.LayerPainter
	{
		private int width;

		public UnderlineHighlightPainter(Color c, int width)
		{
			color = c;
			this.width = width;
            stroke = new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL, 10.0f, new float[]{1,3}, 0);
        }

		public void paint(Graphics g, int offs0, int offs1, Shape bounds,
		                  JTextComponent c)
		{
			// Do nothing: this method will never be called
		}

		public Shape paintLayer(Graphics g, int offs0, int offs1, Shape bounds,
		                        JTextComponent c, View view)
		{

            Rectangle alloc = null;
			if (offs0 == view.getStartOffset() && offs1 == view.getEndOffset())
			{
				if (bounds instanceof Rectangle)
				{
					alloc = (Rectangle) bounds;
				}
				else
				{
					alloc = bounds.getBounds();
				}
			}
			else
			{
				try
				{
					Shape shape = view.modelToView(offs0,
							Position.Bias.Forward, offs1,
							Position.Bias.Backward, bounds);
					alloc = (shape instanceof Rectangle) ? (Rectangle) shape
							: shape.getBounds();
				}
				catch (BadLocationException e)
				{
					return null;
				}
			}

			FontMetrics fm = c.getFontMetrics(c.getFont());
			int baseline = alloc.y + alloc.height - fm.getDescent() + 1;
			for (int i = 0; i < width; i++)
            {
/*                Graphics2D g2d = (Graphics2D) g;
			g.setColor(color == null ? c.getSelectionColor() : color);

//                g.drawLine(alloc.x, baseline + i, alloc.x + alloc.width, baseline + i);
                Stroke s = g2d.getStroke();
                g2d.setStroke(stroke);
                g2d.drawLine(alloc.x, baseline + i, alloc.x + alloc.width, baseline + i);
                g2d.drawLine(alloc.x+2, baseline + i+1, alloc.x + alloc.width, baseline + i+1);
                  g2d.setStroke(s);

*/            }

            return alloc;
		}

		protected Color color; // The color for the underline
        private BasicStroke stroke;
    }
}